package club.weiyuanstudio.java.eedemo.mybatisdemo.service.strategy;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Aspect
@Component
public class InsertBookNameChecker {
    @Pointcut("execution(* club.weiyuanstudio.java.eedemo.mybatisdemo.service.BookService.addBook(String, String, java.util.Date, Double))")
    private void bookInsert() {
    }

    @Before("bookInsert()")
    private void beforeBookInsert(JoinPoint joinPoint) {
        log.debug("Before Insert>>>>");

        String bookName = (String) joinPoint.getArgs()[0];

        log.debug("Book Name Is:" + joinPoint.getArgs()[0]);

        String[] keyWords = {"TEST", "key word"}; // key word list
        boolean isIllegal = Arrays.asList(keyWords).contains(bookName);

        if (isIllegal) throw new IllegalBookNameException("Book Name can't be" + bookName);

    }

    @After("bookInsert()")
    private void afterBookInsert() {
        log.debug("Book Insert Success<<<<");
    }
}
